package cn.edu.nju.software.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class JDBCUtil {

	public static DataSource getDataSource() throws NamingException {
		Context ctx = new InitialContext();
		return (DataSource) ctx.lookup("java:/comp/env/jdbc/BookMarketDB");
	}
	
	public static void closeResultSet(ResultSet rs) throws SQLException {
		if (rs != null) {
			rs.close();
		}
	}
	
	public static void closeStatement(Statement stmt) throws SQLException {
		if (stmt != null) {
			stmt.close();
		}
	}
	
	public static String assemblePattern(String str) {
		return "%" + str + "%";
	}
	
	public static void closeConnection(Connection con) throws SQLException {
		if (con != null) {
			con.close();
		}
	}
	
	public static final int VIOLATION_OF_PK_CONSTRAINT = 2627;
	
	public static final int VIOLATION_OF_UQ_CONSTRAINT = 2627;
	
	public static final int VIOLATION_OF_FK_CONSTRAINT = 547;
}
